/**
 * <pre>
 * ss-fire-collector / ConfigTask.java
 * collector.core Class
 * ConfigTask
 * <br>
 * 
 * <br>
 * @author ytjeong/apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 12.
 * </pre>
 */
package collector.core;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import test.collector.config.useConfigModuleTest;

import collector.core.config.ConfigModule;
import collector.core.util.StringUtil;

/** <pre>
 * Class : collector.core.ConfigTask / ConfigTask.java
 * 	Define Class Object : Task Constants로 read된 객체에 따라 모듈별 정보를 담는다.
 * 	XML Config에서 load 된 정보를 Task List에 담는다.
 * <br>
 * </pre>
 * @author ytjeong / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 12.
 */

public class ConfigTask {
    
    private static Logger _log = LoggerFactory.getLogger(useConfigModuleTest.class);
    private static final String _LogPrefixStr = CollectConstants.LOGGER_MODULE_NAME_CMS + CollectConstants.LOGGER_MODULE_NAME_AFTFIX;
	
    private static Map<String, TaskConstVO> tConVoMap = new HashMap<String, TaskConstVO>();
    
	private static final class SingletonHolder {
		static final ConfigTask singleton = getConfigTask();
		private static ConfigTask getConfigTask() {
		    ConfigTask ct = new ConfigTask();
		    ct.loadConfigTask();
			return ct;
		}
	}

    
    public static ConfigTask newInstance() {
	return SingletonHolder.singleton;
    }
    
    /**
     * <pre>
     * Method : ConfigTask.loadConfigTask()
     * 
     * WebKeeper	12회 / 1일	01H 30M
     * MSG-i	1회 / 1일	04H
     * DB-i	1회 / 1일	02H
     * Privacy-i	1회 / 1일	23H
     * NET-KEY (Active)	24회 / 1일	00H
     * NET-KEY (Active)	24회 / 1일	00H
     * REDCASTLE	1회 / 1일	01H
     * POWERBROKER	1회 / 1일	01H
     * Tx Logging	1회 / 1일	01H
     * Potal Login	1회 1일	01H
     * <br>
     * 	return type of void
     * <br> 
     * </pre>
     */
    public void loadConfigTask() {
	TaskConstVO tConVo = null;
	String taskName = null;
	String taskPathCurrent = null;
	String taskPathHistory = null;
	String taskPathState = null;
	String taskGroup = null;
	String taskClassStr = null;

	// 설정 정보의 활용 가이드
	// 1. 설정 정보를 획득하기 이전에 설정 정보의 클래스를 얻어온다.
	ConfigModule configModule = ConfigModule.getConfig(ConfigModule.class);

	// 개별 Task 모듈 로드
	_log.info(_LogPrefixStr + "----- loaing ConfigTask -------------------");
	for ( TaskConstVO tcvo : configModule.getModuleTaskList() ) {
	    
	    if ( StringUtil.isEmpty(tcvo.getUseYN()) || "Y".equals(tcvo.getUseYN()) ) {
	    	taskName = tcvo.getTaskName();
	    	taskPathCurrent = tcvo.getTaskPathCurrent();

	    	taskPathHistory = tcvo.getTaskPathHistory();
	    	if ( StringUtil.isEmpty(taskPathHistory) )
	    		taskPathHistory = taskPathCurrent + CollectConstants.DEFAULT_TASK_HISTORYNAME_AFTFIX;//"/history/";

	    	taskPathState = tcvo.getTaskPathState();
	    	if ( StringUtil.isEmpty(taskPathState) )
	    		taskPathState = taskPathCurrent + CollectConstants.DEFAULT_TASK_STATENAME_AFTFIX;//"/state/";

	    	taskGroup = tcvo.getTaskGroup();
	    	if ( StringUtil.isEmpty(taskGroup) )
	    	    taskGroup = taskName+CollectConstants.DEFAULT_TASK_GROUPNAME_AFTFIX;//"_Group";
	    	
	    	taskClassStr = tcvo.getTaskClassStr();
	    	if ( StringUtil.isEmpty(taskClassStr) ) 
	    	    taskClassStr = CollectConstants.DEFAULT_TASK_CLASSSTR_PREFIX
	    		 		+ taskName+CollectConstants.DEFAULT_TASK_CLASSSTR_AFTFIX;	// collector.module.ModuleJob
	    	
		//String taskName, String taskIdx,String taskPathOriginal, String taskPathHistory, String taskPathState, String taskGroup,String taskClassStr, String scheduleDateStr,String durationType, int durationValue
		tConVo = new TaskConstVO(taskName, tcvo.getTaskIdx(), tcvo.getUseYN(), tcvo.getTaskPathCurrent(), tcvo.getTaskPathHistory(), tcvo.getTaskPathState(), 
							taskGroup, taskClassStr, tcvo.getScheduleDateStr(), tcvo.getDurationType(), tcvo.getDurationValue());
		tConVoMap.put(taskName, tConVo);
	    }
	}
	_log.info(_LogPrefixStr + "----- loaded ConfigTask " + " [ current enabled : "+ tConVoMap.size() + " 건 / total : " + configModule.getModuleTaskList().size() + " 건 ] -------------------");
	return;
    }

    /** <pre>
     * Method : ConfigTask.gettConVoList()
     * 	to get tConVoList
     * <br> 
     * @return the tConVoList : type of List<TaskConstVO>
     * </pre>
     */
    public Map<String, TaskConstVO> getConVoMap() {
        return tConVoMap;
    }

    /**
     * <pre>
     * Method : ConfigTask.setConVoList()
     * 
     * <br>
     * 	return type of void
     * <br>
     * @param tConVoList 
     * </pre>
     */
    public void setConVoMap(Map<String, TaskConstVO> tConVoMap) {
        ConfigTask.tConVoMap = tConVoMap;
    }

    /**
     * <pre>
     * Method : ConfigTask.getTaskConst()
     * 
     * <br>
     * 	return type of TaskConstVO
     * <br>
     * @param pTaskID
     * @return 
     * </pre>
     */
    public TaskConstVO getTaskConst(String pTaskID) {
	if ( tConVoMap != null) return tConVoMap.get(pTaskID);
	return null;
    }

    
    /* (non-Javadoc)
     * @see java.lang.Object#toString() 
     * 
     * Method : collector.core.ConfigTask.toString()
     */
    public String toString() {
	StringBuilder builder = new StringBuilder();
	builder.append("ConfigTask [tConVoMap=")
	.append(tConVoMap)
	.append("]");
	return builder.toString();
    }
    
}
